# How to Install LAMPP Stack on Debian/Ubuntu [Experimental]

Ubuntu is based on Debian so it should apply to Ubuntu as well. I have tested this on latest Debian Testing (on SparkyLinux Rolling). I usually use WordPress and CakePHP on my localhost so I included instructions at the end to make them work perfectly.

__Warning:__ This config is not written with security in mind, and is only prepared for convenience of desktop users. If you are setting up for server, please refer to proper manuals.

__Note:__ This config is experimental. There might be better way to do some aspects. But it does work for my specific needs so I made it available. Please use carefully.


## Step 1: Install stuff

Install Apache and Mysql/Mariadb:

`sudo apt install apache2 mariadb-server`

Install PHP and other required libraries:

`sudo apt install php php-intl php-mysql`

Install phpMyAdmin:

`sudo apt install phpmyadmin`


__Note:__ Remember to press space to select "apache2" (not lighttpd) in the screen that comes, then press tab and enter. If asks for "Configure database for phpmyadmin with dbconfig-common?" I chose no.

##### E: Package 'phpmyadmin' has no installation candidate

Recently found out Debian Testing does not have the phpMyAdmin package on the official repos anymore. So it issues this message above. Skip this if you don't see this message. [Download phpMyAdmin from here](https://www.phpmyadmin.net/downloads/) and then keep it on /var/www/html/phpmyadmin. Go inside the folder and copy `config.sample.inc.php` to `config.inc.php`. Edit this file instead where it is told to edit `/etc/phpmyadmin/config.inc.php` from now on. When a new update arrives for phpMyAdmin, install Composer with `sudo apt install composer` or [see here](https://getcomposer.org/download/) if not already, `cd` to the directory and run `composer update`.

Also, install recommended packages for phpMyAdmin:
```
sudo apt install php-mbstring php-zip php-gd php-gettext
```


Now, to start the local server:

`sudo systemctl start apache2 mariadb`

To test, open `http://localhost` on a webbrowser and then test `http://localhost/phpmyadmin`. They should open without problems. If any message is given, follow the headings for the message under **Step 2** below.


## Step 2: Configuring


### `/phpmyadmin` not found

If you've installed from repo:
```
ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
```

If you've downloaded phpMyAdmin, then:
`sudo nano /etc/apache2/sites-available/phpmyadmin.conf`
```
Alias /phpmyadmin "/var/www/html/phpmyadmin"
<Directory "/var/www/html/phpmyadmin">
    DirectoryIndex index.php
    AllowOverride All
    Options FollowSymlinks
    Require all granted
</Directory>
```

```
sudo a2ensite phpmyadmin.conf
sudo systemctl reload apache2
```


---

If you can't access `http://localhost/phpmyadmin` then run:

`sudo nano /etc/apache2/apache2.conf`

then add at the end:
```
Include /etc/phpmyadmin/apache.conf
```

then:

`sudo systemctl restart apache2`


### Cannot login to phpMyAdmin (root, with no password)

Windows XAMPP has root user with no password. To keep it here same:

Run:

`sudo nano /etc/phpmyadmin/conf.d/custom.inc.php`

then add these:

```
if (!empty($dbname)) {
	$i--;
	$cfg['Servers'][$i]['user'] = 'root';
	$cfg['Servers'][$i]['AllowNoPasswordRoot'] = TRUE;
	$cfg['Servers'][$i]['AllowNoPassword'] = TRUE;
	$i++;
}
$cfg['Servers'][$i]['AllowNoPassword'] = TRUE;
```

Also, comment these lines (maybe not needed):
```
//$cfg['Servers'][$i]['controluser'] = $dbuser;
//$cfg['Servers'][$i]['controlpass'] = $dbpass;
```

To clear mysql root password (to use blank password), run:
`sudo mysql -u root`

then a mysql shell will open. Enter these on the shell:
```
use mysql;
update user set plugin='' where User='root';
flush privileges;
\q
```

source: https://superuser.com/a/1027838


### Cannot login to PhpMyAdmin (root, with a password)

If you want to set a password for root user then follow this. If you don't want to set a password, skip to next heading.

You can skip this if you did not set a password for phpMyAdmin. It may be that it is not possible to login to phpMyAdmin with root and with a password after install. This is normal since password is not set yet. To set a password, run:

`sudo mysql_secure_installation`

Go through the process and set the password. If it does not work then:

```
sudo systemctl stop mariadb
sudo mysqld --skip-grant-tables &
sudo mysql -u root mysql
```

Replace YOURNEWPASSWORD with your new password:

`UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit;`

`sudo systemctl start mariadb`

Now you should be able to login with the new password on root user.

source: https://askubuntu.com/a/893899

### Cannot login to PhpMyAdmin (separate user, with a password)

It is recommended to not use or modify root because it may be changed on upgrades. You can create a new user called something like `admin` to maintain databases. Run `sudo mysql_secure_installation` but when asked to set a root password, choose `n`. Run:

```
sudo mysql
# then on the prompt:
GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'yourmariadbpasswd' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit
```

Run `mysqladmin -u admin -p version` and enter the password you set. It should return version info if successful.

https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-debian-10

### Using Adminer

Adminer can be a lightweight and yet feature rich alternative to phpMyAdmin.

`sudo apt install adminer`

If you can't access `http://localhost/adminer` then...

`sudo nano /etc/apache2/conf-available/adminer.conf`

```
Alias /adminer "/usr/share/adminer/adminer"
<Directory "/usr/share/adminer/adminer">
    DirectoryIndex index.php
    AllowOverride All
    Options FollowSymlinks
    Require all granted
</Directory>
```

`sudo a2enconf adminer.conf`

`sudo systemctl restart apache2`

Unfortunately, it complains if there is no password on mysql root user:

`Adminer does not support accessing a database without a password...`

Set a mysql root password or follow the instructions on [the link with the message](https://www.adminer.org/en/password/).

source: https://www.techrepublic.com/article/how-to-make-mysql-administration-simple-with-adminer/


### WordPress plugin cannot install - wants FTP credentials

To solve this problem, run:

`sudo groupadd www-data` # maybe not needed, it should be automatically created w/apache

`sudo usermod -a -G www-data $USER` # to add your user to www-data group

Run:

`sudo nano /etc/apache2/envvars`

then add:

```
export APACHE_RUN_USER=your_username
```

* replace `your_username` with your actual username (output of `whoami` command).

`sudo systemctl restart apache2`


### Enable mod_rewrite

`sudo nano /etc/apache2/sites-available/20-www.conf`
```
<Directory "/var/www/html">
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
</Directory>
```

Then:
```
sudo a2ensite 20-www.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
```


### Start/stop/restart server

```
sudo systemctl start apache2 mariadb
sudo systemctl stop apache2 mariadb
sudo systemctl restart apache2 mariadb
```


### Get loaded php.ini file path:

`php --ini | grep Loaded | awk '{ print $4 }'`

Mine says `/etc/php/7.2/cli/php.ini` so I used it on later instructions.


### Enable PHP error logging

sudo nano /etc/php/7.3/cli/conf.d/30-log.ini
```
error_log = /var/log/apache2/error.log
```

Then:

```
sudo systemctl restart apache2
php --info | grep error  # to confirm
```


### CakePHP preparation:

```
sudo apt install composer php-sqlite3
sudo phpenmod intl
sudo phpenmod pdo_mysql
sudo phpenmod pdo_sqlite
sudo phpenmod sockets
sudo phpenmod mbstring
```

Then:
`sudo systemctl restart apache2`

source: https://stackoverflow.com/a/29405081


## Step 3: Convenience

`ln -s /var/www/html ~/html`

`sudo chown -R $USER:www-data /var/www/html/`

This will create a shortcut named `html` on your home and allow you to modify files without root, like a normal directory.

As this is an experimental config, please report any issues.
